package com.zhou.springboot;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * author : zhoujb
 * created : 2020/6/28.
 * QQ : 1377072049
 */

public class GlobalException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Exception ex) {
        ModelAndView mv = new ModelAndView();
        //判断不同异常类型，做不同视图跳转
        if(ex instanceof ArithmeticException){
            System.out.println("=====Error1");
            mv.setViewName("error");
        }
        if(ex instanceof NullPointerException){
            System.out.println("=====Error2");
            mv.setViewName("error");
        }
        System.out.println("测试Excption========");
        mv.addObject("error", ex.toString());
        return mv;
    }
}
